package object;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

/**
 * Class containing information about the scrolling background object
 *
 */

public class Scroll
{
    private BufferedImage bufImage;

    private int imWidth;
    private int pWidth,pHeight;
    private int moveSize;
    private int imTop;

    private boolean isMovingLeft;
    private boolean isMovingRight;

    public Scroll(BufferedImage buf,int width,int height,int move)
    {
        bufImage=buf;
        pWidth=width;
        pHeight=height;
        imWidth=bufImage.getWidth();
        moveSize=move;
        imTop=0;
        isMovingRight=false;
        isMovingLeft=false;
    }

    public void update()
    {
        if (isMovingRight)
            imTop=(imTop+moveSize)%imWidth;
        else
            if (isMovingLeft)
                imTop=(imTop-moveSize)%imWidth;
    }

    private void draw(Graphics g,BufferedImage buf,int screenX1,int screenX2,int imageX1,int imageX2)
    {
        g.drawImage(buf,screenX1,0,screenX2,pHeight,imageX1,0,imageX2,pHeight,null);
    }

    public void display(Graphics g)
    {
        if (imTop==0)
            draw(g,bufImage,0,pWidth,0,pWidth);
        else
            if ((imTop>0)&&(imTop<pWidth))
            {
                draw(g,bufImage,0,imTop,imWidth-imTop,imWidth);
                draw(g,bufImage,imTop,pWidth,0,pWidth-imTop);
            }
            else
                if (imTop>= pWidth)
                    draw(g,bufImage,0,pWidth,imWidth-imTop,imWidth-imTop+pWidth);
                else
                    if ((imTop<0)&&(imTop>=pWidth-imWidth))
                        draw(g,bufImage,0,pWidth,-imTop, pWidth-imTop);
                    else
                        if (imTop< pWidth-imWidth)
                        {
                            draw(g,bufImage,0,imWidth+imTop,-imTop,imWidth);
                            draw(g,bufImage,imWidth+imTop,pWidth,0,pWidth-imWidth-imTop);
                        }
    }

    public void moveRight()
    {
        isMovingLeft=false;
        isMovingRight=true;
    }

    public void moveLeft()
    {
        isMovingLeft=true;
        isMovingRight=false;
    }

    public void stayStill()
    {
        isMovingLeft=false;
        isMovingRight=false;
    }

}
